package com.bj58.mis.apm.server.kylin.common.hllc;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HadoopUtil {
    private static final Logger logger = LoggerFactory.getLogger(HadoopUtil.class);
    private static final transient ThreadLocal<Configuration> hadoopConfig = new ThreadLocal();

    public static void setCurrentConfiguration(Configuration conf) {
        hadoopConfig.set(conf);
    }

    public static Configuration getCurrentConfiguration() {
        Configuration conf = hadoopConfig.get();
        if (conf == null) {
            //conf = healSickConfig(new Configuration());
            conf = new Configuration();
            setCurrentConfiguration(conf);
        }
        return conf;
    }

    public static Configuration healSickConfig(Configuration conf) {
        if (StringUtils.isBlank(conf.get("hadoop.tmp.dir"))) {
            conf.set("hadoop.tmp.dir", "/tmp");
        }

        if (StringUtils.isBlank(conf.get("hbase.fs.tmp.dir"))) {
            conf.set("hbase.fs.tmp.dir", "/tmp");
        }

        conf.set("yarn.timeline-service.enabled", "false");
        return conf;
    }

    public static String fixWindowsPath(String path) {
        if (path.startsWith("file://") && !path.startsWith("file:///") && path.contains(":\\")) {
            path = path.replace("file://", "file:///");
        }

        if (path.startsWith("file:///")) {
            path = path.replace('\\', '/');
        }

        return path;
    }
}
